package com.earl.eatbao.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.earl.eatbao.commons.dao.BaseRepsitoty;
import com.earl.eatbao.commons.service.BaseServiceImpl;
import com.earl.eatbao.dao.eatbao.AddressDao;
import com.earl.eatbao.domain.eatbao.Address;
import com.earl.eatbao.service.AddressService;

@Service("addressService")
public class AddressServiceImpl extends BaseServiceImpl<Address, Long> implements AddressService {

	// desc:日记工具logger
	private static final Logger logger = LoggerFactory.getLogger(AddressServiceImpl.class);

	@Autowired
	private AddressDao addressDao;
	
	@Override
	protected BaseRepsitoty<Address, Long> getDao() {
		return addressDao;
	}

	/**
	 * 查找指定用户默认地址
	 * @param userId 用户id
	 */
	@Override
	public Address findUserDefaultAddress(Long userId) {
        logger.info("<====== 进入findUserDefaultAddress:方法,{[userId] :" + userId + " } ====");
        Address address = addressDao.findUniqueByUserIdAndIsDefaultTrueAndIsDeleteFalse(userId);
        if (null != address) {
			logger.info("==== 退出findUserDefaultAddress方法 返回值类型:{ Address:" + address.toString() + " } =======>");
		} else {
        	logger.info("==== 该用户没有默认地址 ====");
        	logger.info("==== 退出findUserDefaultAddress方法 返回值类型:{ Address: null} =======>");
		}
        	return address;
	}

	@Override
	public List<Address> findUserAllAddress(Long userId) {
		logger.info("<====== 进入findUserAllAddress:方法,{[userId] :" + userId + " } ====");
		List<Address> addressList = addressDao.findByCreatedByAndIsDeleteFalse(String.valueOf(userId));

		logger.info("==== 退出findUserAllAddress方法 返回值类型:{ List<Address>: } =======>");
		return addressList;
	}

	@Override
	public Boolean changeUserDefaultAddress(Long addressId) {
        logger.info("<====== 进入changeUserDefaultAddress:方法,{[userId, addressId] : } ====");
        Boolean result = false;
        Address address = addressDao.findOne(addressId);

        if (null != address && address.getCreatedBy() != null) {
            Address defAddress = addressDao.findUniqueByCreatedByAndIsDefaultTrueAndIsDeleteFalse(address.getCreatedBy());
            if (null != defAddress) {
                defAddress.setDefault(false);
                addressDao.save(defAddress);
            } else {
                logger.info("==== 该用户没有默认收货地址 ====");
            }
            address.setDefault(true);

            addressDao.save(address);
            result = true;
        } else {
            logger.info("==== 该该用户没有对应的地址 ====");
        }

        logger.info("==== 退出changeUserDefaultAddress方法 返回值类型:{ Boolean:" + result + " } =======>");
        return result;
	}

	@Override
	public Boolean deleteAddress(Long addressId) {
		logger.info("<====== 进入deleteAddress:方法,{[addressId] : } ====");
		Boolean result = false;
		if (null != addressId && addressId != 0l) {
			addressDao.delete(addressId);
			result = true;
		} else {
			logger.info("=== 输入的地址ID不合法 ===");
		}
		logger.info("==== 退出deleteAddress方法 返回值类型:{ java.lang.Boolean:  } =======>");
		return result;
	}

	@Override
	public Boolean addAddress(Address address) {

		logger.info("<====== 进入addAddress:方法,{[address] : } ====");
		Boolean result = false;
		if (null != address &&
				address.getUserId() != 0l &&
				address.getPhone().trim().length() != 0) {
			address.setLastModifiedBy(address.getUserId().toString());
			address.setLastModifiedDate(new Date());
			address.setCreatedBy(address.getUserId().toString());
			address.setCreatedDate(new Date());
			address.setDelete(false);
			//如果用户没有默认收货地址添加第一个地址默认为收获地址
			if (null == findUserAllAddress(address.getUserId())) {
				address.setDefault(true);
			}

			addressDao.save(address);
			result = true;
		} else {
			logger.info("=== 新添的收货地址信息不合法 ====" );
		}
		logger.info("==== 退出addAddress方法 返回值类型:{ Boolean: false } =======>");
		return result;
	}
}